﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NMemcached.Client.Test
{
    [TestClass]
    public class ClientTests
    {
        /*
         * The AsyncMemcachedClient is not tested because the methods being tested in the
         * SyncClientTest are the same methods called by the async client. All a unit test
         * would do is prove that the async method invocation in .NET works. I'm pretty
         * sure Microsoft already ensured that it worked in version 1.0
         */

        [TestMethod]
        public void SyncClientTest()
        {
            using (MemcachedClient client = new MemcachedClient(MemcachedSettings.Servers))
            {
                client.IncludeCacheMisses = true;
                client.ConnectionError += new EventHandler<ConnectionEventArgs>(MemcachedSettings.ErrorHandler);

                client.FlushAll();

                ResponseCode first = client.Set("first", "first\r\ntest");
                ResponseCode second = client.Set("second", 42);
                ResponseCode third = client.Set("third", new TestClass("custom object test"));
                ResponseCode fourth = client.Append("third", new TestClass("appended"));

                Assert.AreEqual(ResponseCode.NoError, first);
                Assert.AreEqual(ResponseCode.NoError, second);
                Assert.AreEqual(ResponseCode.NoError, third);
                Assert.AreEqual(ResponseCode.NoError, fourth);

                string[] keys = new string[] { "first", "second", "third", "iDontExist" };

                IDictionary<string, object> result = client.Get(keys);

                Assert.IsNotNull(result);
                Assert.AreEqual(4, result.Count);

                CacheDetail firstDetail = client.GetWithDetail("first");

                if (firstDetail != null)
                {
                    ResponseCode firstCas = client.CheckAndSet("first", "first test cas", firstDetail.CasUnique);
                }

                object casResult = client.Get("first");

                Assert.AreEqual("first test cas", casResult.ToString());

                client.Close();
            }
        }

        [TestMethod]
        public void ClientCompressionTest()
        {
            using (MemcachedClient client = new MemcachedClient(MemcachedSettings.CreateConnectionProvider(true, true)))
            {
                client.ConnectionError += new EventHandler<ConnectionEventArgs>(MemcachedSettings.ErrorHandler);
                client.FlushAll();

                string key = "compressionTestKey";
                string value = "Testing data compression";

                ResponseCode setResponse = client.Set(key, value, DateTime.Now.AddYears(1));

                Assert.AreEqual(ResponseCode.NoError, setResponse);

                object getResult = client.Get(key);

                Assert.IsNotNull(getResult);
                Assert.IsInstanceOfType(getResult, typeof(string));

                Assert.AreEqual(value, getResult.ToString());
            }
        }
    }
}
